#ifndef _MAT_LEVEL_MANAGER_H_
#define _MAT_LEVEL_MANAGER_H_

#include "mat_logicboard.h"
#include "mat_ui_combo.h"
#include "mat_extramove_clearing.h"
#include "mat_gameover_gemcleareffect.h"


struct MATLevelData;

namespace ui
{
	class UIWidget;
	class UIPart2DWidget;
	class UIProgress;
};

namespace gameflow
{
	class UIPopup;
};


class	MATLevelManager 
{
public:

	enum STATE
	{
		STATE_PLAYING,
		STATE_EXTRAMOVE_CLEARING,
		STATE_BOMBEXPLODE,
		STATE_BOMBEXPLODING,
		STATE_ABOUTTOEND,
		STATE_BOARDANIMATE,
		STATE_BOARDANIMATING,
		STATE_ENDING,
		STATE_AFTEREND,
		STATE_COUNT,
	};


	static MATLevelManager& Instance();

	bool					IsLevelSuccess();	
	int						GetStarCount();

	void					Tick(float dt);
	void					NextMove();
	void					CheckLevelComplete();
	void					SetScore(int CurrentScore);
	void					AddMatched(const std::vector<MATGem*>& List);
	void					SetCurrentLevel(int Index, bool LauchNextPeerDashBoard);
	void					ReplayLevel();
	MATLevelData*			GetCurrentLevelData() const { return m_pCurrentData; };
	int						GetCurrentLevel();
	const std::string&		GetFailReason()const { return m_FailReason; };
	int						GetStarScore(int StarIndex) const;	
	void					Render(float Scale);
	bool                    ShoulSpawnTimeBonus() const;
	bool					CanMove() const { return m_State == STATE_PLAYING; };
	void					SetState(STATE State) { m_State = State; };
	bool					IsPlaying() const { return (m_State == STATE_PLAYING); };
	bool					ShouldSpawnMoreBombs() const;
	void					SetGameBackground();

protected:
	
	MATLevelManager();
	~MATLevelManager();

	bool								IsModeTimeOut() const;
	bool								IsModeMove() const;
	bool								IsModeTileClear() const;
	bool								IsModeGem() const;
	void								ShowLevelInfoMsg() const;
	void								SetMusicTheme();	

	void								UpdateTargetGemUI();	
	
	int									m_RemainingMove;
	int									m_MoveCount;
	MATLevelData*						m_pCurrentData;
	std::string							m_FailReason;
	float								m_DealyGameOver;

	ui::UIWidget*						m_pUITimeFrame;
	ui::UIWidget*						m_pUIReMainingMove;
	ui::UIWidget*						m_pUIRemainingTime;
	ui::UIWidget*						m_pUIScoreTarget;
	ui::UIPart2DWidget*					m_pUIPart2dMoveLow;
	ui::UIProgress*						m_pUIProgressTime;
	ui::UIWidget*						m_pUIGemModeFrame;
	ui::UIWidget*						m_pUIScoreModeFrame;
	ui::UIWidget*						m_pUIGemTarget1;
	ui::UIWidget*						m_pUIGemTarget2;
	ui::UIWidget*						m_pUIGemTargetTxt1;
	ui::UIWidget*						m_pUIGemTargetTxt2;

	ui::UIWidget*						m_pUIStarsPoint[3];
	float								m_ProgressWidth;
	
	MATLevelCompletePopup				m_ComboLevelComplete;
	STATE								m_State;

	float								m_NextGemClearDelay;
	float								m_RemainingTime;
	MATGem*								m_pTargetGem1;
	MATGem*								m_pTargetGem2;
	int									m_CountTargetGem1;
	int									m_CountTargetGem2;
	MATExtraMoveClearing				m_ExtraMoveClearing;
	float								m_BombExplodeDelay;
	float								m_BoardAnimateDelay;
	bool								m_IsBombExploded;
	gameflow::UIPopup*					m_pMsgPopup;
	int									m_CurrentLevelIndex;	
	MATClearGem							m_ClearGem;
	float								m_LevelFailPopupDelay;

};




#endif // _MAT_LEVEL_MANAGER_H_